/**
 * 
 */
package com.hera.base.web.vaadin.component;



import java.util.Collection;

import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

/**
 * @author cmontes
 *
 */
public class FormTable extends VerticalLayout implements SimpleSelectable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4690333850596898752L;
	
	private Table control;
	private Label label;
	
	
	public FormTable(){
		super();
		control = new Table();
		label = new Label();
		addComponent(label);
		addComponent(control);
		setSize(200f);
		//control.setEnabled(false);
		control.setHeight("100px");
	}
	
	public FormTable(String value, String styleTextField){
		super();
		control = new Table();
		control.addStyleName(styleTextField);
		this.label = new Label(value);
		addComponent(this.label);
		addComponent(control);
		setId(value +"12");
	}

	@Override
	public void setLabel(String value) {
		// TODO Auto-generated method stub
		label.setValue(value);
	}

	@Override
	public void setSize(Float value) {
		// TODO Auto-generated method stub
		control.setWidth(""+value + "px");
	}

	@Override
	public void setName(String value) {
		// TODO Auto-generated method stub
		setId(value);
	}

	@Override
	public String getLabel() {
		// TODO Auto-generated method stub
		return label.getValue();
	}

	@Override
	public Float getSize() {
		// TODO Auto-generated method stub
		return control.getWidth();
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return getId();
	}
	
	public void addColumnHeader(Object propertyId, Class<?> type, Object defaultValue){
		control.addContainerProperty(propertyId, type, defaultValue);		
	}
	
	public void setColumnWidth(Object propertyId, int width){
		control.setColumnWidth(propertyId, width);
	}
	
	public void addItem(Object[] cells, Object itemId){
		control.addItem(cells, itemId);
	}
	
	public void addItem(Object itemId){
		control.addItem(itemId);
	}
	
	public void removeAllItems(){
		control.removeAllItems();
	}
	
	public void hideColumnControls(){
		
		Collection<String> containers = (Collection<String>) control.getContainerPropertyIds();
		if(!containers.isEmpty()){
			String[] properties = containers.toArray(new String[containers.size()]);
			String[] properties2 = new String[properties.length -1];
			for (int i = 0; i < properties2.length; i++) {
				properties2[i] = properties[i];
			}
			control.setVisibleColumns(properties2);
		}
	}
	
	public void showColumnControls(){
		
		Collection<String> containers = (Collection<String>) control.getContainerPropertyIds();
		if(!containers.isEmpty()){
			String[] properties = containers.toArray(new String[containers.size()]);			
			control.setVisibleColumns(properties);
		}
	}
	
	public void clean(){
		//control.removeAllItems();
		Collection<String> containers = (Collection<String>) control.getContainerPropertyIds();
		if(!containers.isEmpty()){
			String[] properties = containers.toArray(new String[containers.size()]);
			for (int i = 0; i < properties.length; i++) {
				control.removeContainerProperty(properties[i]);
			}			
		}
	}
	
	public void removeColumnControls(){
		control.removeContainerProperty("");		
	}
	
	public void addSampleItem(){
		Collection<String> containers = (Collection<String>) control.getContainerPropertyIds();
		if(!containers.isEmpty()){
			String[] properties = containers.toArray(new String[containers.size()]);
			control.addItem(properties, 1);
		}
	}
	
	public void addSampleItem(HorizontalLayout horizontalLayout){
		Collection<String> containers = (Collection<String>) control.getContainerPropertyIds();
		if(!containers.isEmpty()){
			Object[] properties = containers.toArray(new Object[containers.size()]);
			properties[properties.length-1] = horizontalLayout;
			control.addItem(properties, 1);
		}
	}

}
